<template>
  <!-- 新账号注册容器 -->
  <view class="new-account-container">
    <!-- 主内容区 -->
    <view class="main-content">
      <!-- 应用标识 -->
      <text class="app-name">搭搭子</text>
      <text class="slogan">找到你的玩伴</text>
      
      <!-- 用户头像框 -->
      <view class="avatar-container">
        <image class="user-avatar" src="/static/sample1.jpg"></image>
        <text class="avatar-hint">点击更换头像</text>
      </view>
      
      <!-- 注册表单 -->
      <view class="form-group">
        <input class="form-input" v-model="form.userName" type="text" placeholder="账户" />
      </view>
      <view class="form-group">
        <input class="form-input" v-model="form.password" type="password" placeholder="密码" />
      </view>
      <view class="form-group">
        <input class="form-input" v-model="form.confirmPassword" type="password" placeholder="确认密码" />
      </view>
      
      <!-- 创建按钮 -->
      <button class="create-btn" @click="handleSubmit" :loading="loading">创建用户</button>

      <!-- 昵称输入弹窗 -->
      <uni-popup ref="nicknamePopup" type="dialog">
        <uni-popup-dialog mode="input" title="创建昵称" 
          placeholder="给自己创建一个独一无二的昵称吧"
          @confirm="handleRegister"
          :value="form.nickName"
        ></uni-popup-dialog>
      </uni-popup>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import uniPopup from '@/uni_modules/uni-popup/components/uni-popup/uni-popup.vue'
import uniPopupDialog from '@/uni_modules/uni-popup/components/uni-popup-dialog/uni-popup-dialog.vue'
import { baseUrl , http } from '@/utils/request'

const form = ref({
  userName: '',
  password: '',
  confirmPassword: '',
  nickName: ''
})
const loading = ref(false)
const nicknamePopup = ref(null)

const handleSubmit = () => {
  if (!form.value.userName || !form.value.password) {
    uni.showToast({ title: '请输入账号和密码', icon: 'none' })
    return
  }
  if (form.value.password !== form.value.confirmPassword) {
    uni.showToast({ title: '两次密码不一致', icon: 'none' })
    return
  }
  nicknamePopup.value.open()
}

const handleRegister = async (nickName) => {
  if (!nickName) {
    uni.showToast({ title: '请输入昵称', icon: 'none' })
    return
  }
  form.value.nickName = nickName
  
  try {
    loading.value = true
    console.log(form.value);
    
    const res = await uni.request({
      url: baseUrl + '/users/register',
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        nickName: form.value.nickName,
        userName: form.value.userName,
        password: form.value.password
      }
    })
    // const res = await uni.request({
    //   url: baseUrl + `/users/register?nickName=${form.value.nickName}&password=${form.value.password}&userName=${form.value.userName}`,
    //   method: 'POST'
    // })
    uni.showToast({ title: '注册成功' })
    uni.navigateBack();
  } catch (err) {
    uni.showToast({ title: '注册失败：'+err, icon: 'none' })
  } finally {
    loading.value = false
  }
}
</script>

<style>
.new-account-container {
  height: 100vh;
  background: #ffffff;
  position: relative;
}

/* 主内容区 */
.main-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 10vh;
}

/* 应用标识 */
.app-name {
  font-size: 64rpx;
  color: #FF2442;
  font-weight: bold;
  margin-bottom: 16rpx;
}

.slogan {
  font-size: 38rpx;
  color: #666;
  margin-bottom: 60rpx;
}

/* 用户头像框 */
.avatar-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 60rpx;
}

.user-avatar {
  width: 160rpx;
  height: 160rpx;
  border-radius: 50%;
  margin-bottom: 20rpx;
}

.avatar-hint {
  font-size: 28rpx;
  color: #999;
}

/* 表单组 */
.form-group {
  margin-bottom: 40rpx;
  width: 560rpx;
}

.form-input {
  width: 100%;
  height: 96rpx;
  border: 1rpx solid #eee;
  border-radius: 48rpx;
  padding: 0 32rpx;
  font-size: 32rpx;
}

/* 创建按钮 */
.create-btn {
  background: #FF2442;
  color: #ffffff;
  width: 560rpx;
  height: 96rpx;
  border-radius: 48rpx;
  font-size: 32rpx;
  line-height: 96rpx;
  margin-top: 40rpx;
}
</style>
